package com.miniapp.web.controller.miniapp.api;

import com.alibaba.fastjson.JSONObject;
import com.miniapp.basics.entity.R;
import com.miniapp.basics.service.IMiniAppUserService;
import com.miniapp.common.config.MiniappConfig;
import com.miniapp.common.core.controller.BaseController;
import com.miniapp.common.core.domain.AjaxResult;
import com.miniapp.common.core.domain.entity.miniapp.MiniAppUser;
import com.miniapp.common.core.domain.entity.miniapp.dto.MiniAppUserDTO;
import com.miniapp.common.core.domain.model.LoginUser;
import com.miniapp.common.exception.MiniAppException;
import com.miniapp.common.utils.SecurityUtils;
import com.miniapp.common.utils.file.FileUploadUtils;
import com.miniapp.common.utils.file.MimeTypeUtils;
import com.miniapp.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/miniapp")
public class WxMiniAppUserController extends BaseController
{
    @Autowired
    private IMiniAppUserService miniAppUserService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取微信小程序用户信息
     * @return
     */
    @GetMapping("/getMiniAppUserInfo")
    public R getMiniAppUserInfo()
    {

        return R.success(getLoginUser().getMiniAppUser());
    }

    /**
     * 更新微信小程序用户信息
     * @param miniAppUserDTO
     * @return
     */
    @PostMapping("/updateMiniAppUser")
    public R updateMiniAppUser(@RequestBody MiniAppUserDTO miniAppUserDTO)
    {
        LoginUser loginUser = getLoginUser();
        //这里防止被人恶意发包修改 通过Security获取当前的id
        miniAppUserService.updateMiniAppUser(new MiniAppUser(miniAppUserDTO,loginUser.getMiniAppUser().getId()));
        //更新缓存
        loginUser.getMiniAppUser().setNickName(miniAppUserDTO.getNickName());
        loginUser.getMiniAppUser().setSex(miniAppUserDTO.getSex());
        tokenService.setLoginUser(loginUser);
        return R.success();
    }

    @PostMapping("/uploadAvatar")
    public R uploadAvatar(@RequestParam("avatarfile") MultipartFile file) throws MiniAppException
    {
        LoginUser loginUser = getLoginUser();
        JSONObject data = miniAppUserService.uploadAvatar(loginUser.getMiniAppUser(), file);
        return R.success(data);
    }
}
